package main

import (
	"fmt"
	"time"
)

func main() {
	var intChan chan int
	intChan = make(chan int, 19)
	exitChan := make(chan bool)
	go write(intChan)
	go read(intChan, exitChan)
	for {
		ifExit, ok := <-exitChan
		fmt.Println(ok)
		if ifExit {
			break
		}
	}
}

func write(intChan chan<- int) {
	defer func() {
		if err := recover(); err != nil {
			fmt.Println("error occurs")
		}
	}()
	i := 0
	for {
		i++
		intChan <- i
		fmt.Printf("write %d \n", i)
		time.Sleep(time.Millisecond * 99)
		if i >= 10 {
			break
		}
	}
	close(intChan)
}

func read(intChan <-chan int, exitChan chan<- bool) {
	for {
		v, isNotClosed := <-intChan
		if !isNotClosed {
			fmt.Println(isNotClosed)
			break
		}
		fmt.Printf("read %d \n", v)
	}
	exitChan <- true
	close(exitChan)
}
